Q 4:
Analyze and write a program to display series: 0 1 1
2 3 5 8 13 21 34 55 89.
The Fibonacci sequence is a series where the next term is the sum of pervious two terms. The first two terms of the Fibonacci sequence is 0 followed by 1.
By definition the first two numbers are:
Fibonacci(0) = 0 =>t1
Fibonacci(1) = 1 =>t2
The next number is always the sum of the previous two. Fibonacci(n) = Fibonacci(n-1) + Fibonacci(n-2)
Fibonacci(2) = 0 + 1 = 1 => nextTerm =t1+t2; t1 = t2; t2 = nextTerm;
Fibonacci(3) = 1 + 1 = 2 => nextTerm =t1+t2; t1 = t2; t2 = nextTerm;
Fibonacci(4) = 1 + 2 = 3 => nextTerm =t1+t2; t1 = t2; t2 = nextTerm;
Fibonacci(5) = 2 + 3 = 5 => nextTerm =t1+t2; t1 = t2; t2 = nextTerm;
Fibonacci(6) = 3 + 5 = 8 => nextTerm =t1+t2; t1 = t2; t2 = nextTerm;
Fibonacci(7) = 5 + 8 = 13 => nextTerm =t1+t2; t1 = t2; t2 = nextTerm;
Fibonacci(8) = 8 + 13 = 21 => nextTerm =t1+t2; t1 = t2; t2 = nextTerm;
Fibonacci(9) = 13 + 21 = 34 => nextTerm =t1+t2; t1 = t2; t2 = nextTerm;
Fibonacci(10) = 21 + 34 = 55 => nextTerm =t1+t2; t1 = t2; t2 = nextTerm;
Fibonacci(11) = 34 + 55 = 89 => nextTerm =t1+t2; t1 = t2; t2 = nextTerm;
Program
#include
<stdio.h>
void main()
{
int
i, n, t1 = 0, t2 = 1, nextTerm;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (i = 1; i <= n; i++)
{
printf("%d, ", t1);
nextTerm
= t1 + t2;
t1 = t2;
t2 = nextTerm;
}
}